package com.key.w8.http1.connection.reconnect;

import com.key.w8.http1.connection.environment.ClientPoolEnvironment;
import com.key.w8.http1.connection.connectionImpl.ClientHttp1PersistentConnection;
import com.key.w8.http1.connection.reconnect.connectionImpl.ClientReconnectSupportConnection;
import com.key.w8.http1.connection.pool.ClientConnectionBuilder;
import com.key.w8.http1.handler.codec.CodecBuilder;
import io.netty.channel.EventLoopGroup;

import java.util.concurrent.TimeUnit;

/**
 * @Author: k8
 * @CreateTime: 2025-03-22
 * @Version: 1.0
 */
public class ReConnectSupportConnectionBuilder extends ClientConnectionBuilder {
    /**
     * netty默认的负债均衡也是通过一个atomicInteger进行轮询选择
     * 可以自定义，但是灵活性不够
     * 如果直接轮询，当有连接关闭后又创建
     * 容易导致不同的连接被注册到同一个eventLoop上
     * 所以使用单线程group，创建多个，自定义轮询
     *
     * @param environment
     * @param codecBuilder
     */
    public ReConnectSupportConnectionBuilder(ClientPoolEnvironment environment, CodecBuilder codecBuilder) {
        super(environment, codecBuilder);
    }

    @Override
    protected ClientHttp1PersistentConnection getConnection(ClientPoolEnvironment environment, EventLoopGroup group) {
        return new ClientReconnectSupportConnection(environment.getResponseTimeOut(),TimeUnit.MILLISECONDS,environment.getReConnectTimes(),group,this);
    }
}
